﻿using AiChiTu.TraceabilitySystem.Infrastructure.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AiChiTu.TraceabilitySystem.Infrastructure.Implements
{
	/// <summary>
	/// 泛型仓储实现
	/// </summary>
	/// <typeparam name="T">实体</typeparam>
	public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
	{
		/// <summary>
		/// 数据库上下文
		/// </summary>
		private readonly DataDBContext db;
		/// <summary>
		/// 构造方法
		/// </summary>
		/// <param name="db">数据库上下文</param>
		public BaseRepository(DataDBContext db)
		{
			this.db = db;
		}
		/// <summary>
		///	添加多条数据
		/// </summary>
		/// <param name="entities">集合</param>
		/// <returns>返回任务</returns>
		public async Task<bool> CreateEntitiesAsync(IEnumerable<T> entities)
		{
			await db.Set<T>().AddRangeAsync(entities);
			return await db.SaveChangesAsync() > 0;
		}
		/// <summary>
		/// 添加单条数据
		/// </summary>
		/// <param name="entity">实体</param>
		/// <returns>返回任务</returns>
		public async Task<bool> CreateEntityAsync(T entity)
		{
			await db.Set<T>().AddAsync(entity);
			return await db.SaveChangesAsync() > 0;
		}
		/// <summary>
		/// 删除多条数据
		/// </summary>
		/// <param name="entities">集合</param>
		/// <returns>返回任务</returns>
		public async Task<bool> DeletedEntitiesAsync(IEnumerable<T> entities)
		{
			db.Set<T>().RemoveRange(entities);
			return await db.SaveChangesAsync() > 0;
		}
		/// <summary>
		/// 删除单条数据
		/// </summary>
		/// <param name="entity">实体</param>
		/// <returns>返回任务</returns>
		public async Task<bool> DeletedEntityAsync(T entity)
		{
			db.Set<T>().Remove(entity);
			return await db.SaveChangesAsync() > 0;
		}
		/// <summary>
		/// 延迟查询全部数据
		/// </summary>
		/// <returns>返回任务</returns>
		public IQueryable<T> QueryAllAsync()
		{
			return db.Set<T>().AsQueryable();
		}
		/// <summary>
		/// 延迟条件查询全部数据
		/// </summary>
		/// <param name="expression">Lambda表达式</param>
		/// <returns>返回任务</returns>
		public IQueryable<T> QueryAllAsync(Expression<Func<T, bool>> expression)
		{
			return db.Set<T>().Where(expression).AsQueryable();
		}
		/// <summary>
		/// 查询单条数据
		/// </summary>
		/// <param name="id">主键ID</param>
		/// <returns>返回任务</returns>
		public async Task<T?> QueryEntityAsync(int id)
		{
			T? entity = await db.Set<T>().FindAsync(id);
			return entity;
		}
		/// <summary>
		/// 条件查询单条数据
		/// </summary>
		/// <param name="expression">Lambda表达式</param>
		/// <returns>返回任务</returns>
		public async Task<T?> QueryEntityAsync(Expression<Func<T, bool>> expression)
		{
			T? entity = await db.Set<T>().Where(expression).FirstOrDefaultAsync();
			return entity;
		}
		/// <summary>
		/// 修改多条数据
		/// </summary>
		/// <param name="entities">集合</param>
		/// <returns>返回任务</returns>
		public async Task<bool> UpdateEntitiesAsync(IEnumerable<T> entities)
		{
			db.Set<T>().UpdateRange(entities);
			return await db.SaveChangesAsync() > 0;
		}
		/// <summary>
		/// 修改单条数据
		/// </summary>
		/// <param name="entity">实体</param>
		/// <returns>返回任务</returns>
		public async Task<bool> UpdateEntityAsync(T entity)
		{
			db.Set<T>().Update(entity);
			return await db.SaveChangesAsync() > 0;
		}



		/// <summary>
		/// 实际查询全部数据
		/// </summary>
		/// <returns>返回任务</returns>
		public async Task<List<T>> GetListAsync()
		{
			return await db.Set<T>().ToListAsync();
		}
		/// <summary>
		/// 条件查询全部数据
		/// </summary>
		/// <param name="expression">Lambda表达式</param>
		/// <returns>返回任务</returns>
		public async Task<List<T>> GetListAsync(Expression<Func<T, bool>> expression)
		{
			return await db.Set<T>().Where(expression).ToListAsync();
		}
	}
}
